 <?php
include 'base.php';
include 'requireLogin.php';
include 'connectdb.php';
if($_POST){
    if (isset($_POST['type'])) {
        $sql = "DELETE FROM import WHERE code ='".$_POST['code']."'";
        if (mysql_query($sql, $co)){
            echo "Đã xóa thành công!";
        }
        else{                  
                header("HTTP/1.0 404 Not Found");
                header('HTTP', true, 500);
            }
    }           
    else{
        $sql="INSERT INTO import(product_code, import_quantity, date, vendor_code)VALUES('$_POST[product_code]', '$_POST[quantity]', '$_POST[date]', '$_POST[vendor]')";
        if (mysql_query($sql, $co)){
            echo "Hàng đã thêm thành công!";
        }
        else{                  
                header("HTTP/1.0 404 Not Found");
                header('HTTP', true, 500);
            }
        }                
}
?>

<script language="JavaScript" type="text/javascript">

    $(document).ready(function () {
        $('#success, #error, #import_form, #delete').hide();    
        $( "#date" ).datepicker();
        var nClick = 1;
        $("#import").click(function(){ 
            if (nClick > 0)
                $("#import_form").show();
            else
                $("#import_form").hide();
            nClick = (-1)*nClick;
        });

        $(".class-delete").each(function(){
            $(this).click(function(){
                if (!confirm('Bạn có chắc chắn muốn xóa')) return false;                                                
                var code = $(this).attr("name");
                var parent = $(this).closest('tr');
                $.ajax({
                type: "post",
                url: "",
                data: "code=" + code + "&type=delete",
                success: function () {                                        
                    $("#delete").show();
                    $(parent).hide();
                    $("#success").hide();                    
                    $("#error").hide();
                },
                error: function() {                       
                    $('#error').show();  
                    $('#success').hide();                    
                    $("#delete").hide();
                }                
            }); 
            });
        });

        $("#submit").click(function(){           
            if($("#code").val() == '' || $("#name").val() == ''|| $("#quantity").val() == '' || $("#import_price").val() == '' || $("#sale_price").val() == '' || $("#date").val() == ''){
                alert("Chưa điền đủ thông tin");
                return false;
            }
            var product_code = $('#product_code').val(),                
                quantity = $('#quantity').val(),
                
                vendor = $('#vendor').val(),                
                date = $('#date').val();               
                
            $.ajax({
                type: "post",
                url: "",
                data: "product_code=" + product_code + "&quantity=" + quantity + "&sale_price=" + "&date=" + date + "&vendor=" + vendor,
                success: function () {                    
                    $('#error').hide();
                    $("#delete").hide();                
                    $('#success').show();
                    var newRow = $("<tr><td></td><td>" + product_code + "</td><td>" + quantity + "</td><td>" + date + "</td><td>" + vendor + "</td><td></td></tr>");
                    $("#list_table").append(newRow);
                    
                },
                error: function() {                       
                    $('#error').show();
                    $("#delete").hide();  
                    $('#success').hide();                    
                }                
            }); 
            return false;

        });
    });
    
</script>

<div id="submenu" class="btn-toolbar ">
    <div class="btn-group ">
        <a
           title="Nhập danh sách hàng hóa từ file Excel"            
           id="import">
           <i class="icon-arrow-up"></i>
            Nhập từ Excel </a>    
    
            <a
           title="Xuất danh sách hàng hóa ra file Excel."
           href = "export_product_excel.php"           
           id="export">
           <i class="icon-arrow-down"></i>
            Xuất ra Excel </a>        
    </div>
</div>
<div id = "import_form">
    <form enctype="multipart/form-data" action="import_product.php" method="get"> 
      <input type="hidden" name="MAX_FILE_SIZE" value="2000000"/> 
      <p>
      <span>Chọn file</span>
      <span><input type="file" name="file" /></span> 
      <span><input type="submit" value="Tải lên" /></span>    
      </p>       
      <p>Nhập file theo mẫu <a href="book1.xls">này</a></p>
    </form> 
</div>

<table id = "list_table" class="table" style="table-layout:fixed; " >    
    <thead>        
    <tr>        
        <th>Mã</th>    
        <th>Mã sản phẩm</th>        
        <th>Số lượng nhập</th>
        <th>Ngày nhập </th>
        <th>Nhà cung cấp</th>        
        <th></th>
    </tr>
    </thead>

    <tbody>        
        <?php
        include 'nhap_hang_pagination.php';
        if(mysql_num_rows($result)>0){
            while($row = mysql_fetch_array($result))                     
                {                    
                    echo"<tr>";
                    echo"<td><a href=\"nhap_hang_detail.php?code=" . $row['code']."\">".$row['code']."</td>";
                    echo"<td>" . $row['product_code']."</td>";
                    echo"<td>" . $row['import_quantity']."</td>";                                                
                    echo"<td>" . $row['date']."</td>";
                    echo"<td>" . $row['vendor_code']."</td>";                    
                    echo "<td><button class ='btn class-delete' name = '".$row['code']."'>Xóa</button></td>";
                    echo "</tr>";                 
                }
            }                
        ?>
    </tbody>    
    <tr class="form">
        <td></td>      
        <td><select name="product_code" id = "product_code">
            <option value=""selected="selected">Chọn</option>
            <?php
            $sql="select code from product";
            $resultsub=mysql_query($sql);           
            if(mysql_num_rows($resultsub)> 0)            
                while($row = mysql_fetch_array($resultsub))
                {
                ?>          
                    <option value="<?php echo"".$row['code']."";?>"><?php echo"".$row['code']."";?></option>                    
                <?php
                }            
                ?>
        </select>
        </td>
        <td><input type="text" name="quantity" id = "quantity"></td>
        
        <td><input type="text" name="date" id = "date"></td>        
        <td><select name="vendor" id = "vendor">
            <option value=""selected>Chọn NCC</option>
            <?php
            $stSQL="select code from vendor";
            $resultsub=mysql_query($stSQL);           
            if(mysql_num_rows($resultsub)> 0)            
                while($row = mysql_fetch_array($resultsub))
                {
                ?>          
                    <option value="<?php echo"".$row['code']."";?>"><?php echo"".$row['code']."";?></option>                    
                <?php
                }            
                ?>
        </select></td>                
        <td></td>
    </tr>
    <tr>
        
        <td colspan="3">
            <div id = "error">Có lỗi xảy ra, thông tin nhập chưa đúng</div>
            <div id = "success">Đã thêm thành công</div>
            <div id = "delete">Đã xóa thành công</div>
            <input class="submit" type="submit" id = "submit" name="add_product_button" value="Nhập hàng" />
        </td>
        <td colspan="3">
            <?=$pagination?>
        </td>
    </tr>    
</table>         
<?php include 'footer.php';?>